﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;
using MySql.Data.MySqlClient;
using Entities;

namespace BusinessFacade
{
   public class Busi_sys_Role
    {
        public Busi_sys_Role()
        {

        }
        public bool Delete(int id)
        {
            int rows = DbHelperMySql.ExecuteSql("Delete from sys_role where roleid=" + id);
            if(rows>0)
            {
                return true;
            }
            return false;
        }
        public static DataTable Getlist(string RoleName)
        {
            DataSet ds = DbHelperMySql.Query("select * from sys_role where RoleName like '%" + RoleName + "%'");
            return ds.Tables[0];
        }
       public sys_role_Model GetlistModel(int RoleID)
		{
            sys_role_Model model = new sys_role_Model();

            string strSql = "select * from sys_role where RoleID="+RoleID;
			DataSet ds=DbHelperMySql.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["RoleID"]!=null && ds.Tables[0].Rows[0]["RoleID"].ToString()!="")
				{
                    model.RoleID = int.Parse(ds.Tables[0].Rows[0]["RoleID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RoleName"]!=null && ds.Tables[0].Rows[0]["RoleName"].ToString()!="")
				{
                    model.RoleName = ds.Tables[0].Rows[0]["RoleName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateUserID"]!=null && ds.Tables[0].Rows[0]["CreateUserID"].ToString()!="")
				{
                    model.CreateUserID = int.Parse(ds.Tables[0].Rows[0]["CreateUserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateDate"]!=null && ds.Tables[0].Rows[0]["CreateDate"].ToString()!="")
				{
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ModifyUserID"]!=null && ds.Tables[0].Rows[0]["ModifyUserID"].ToString()!="")
				{
                    model.ModifyUserID = int.Parse(ds.Tables[0].Rows[0]["ModifyUserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ModifyDate"]!=null && ds.Tables[0].Rows[0]["ModifyDate"].ToString()!="")
				{
                    model.ModifyDate = DateTime.Parse(ds.Tables[0].Rows[0]["ModifyDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Remark"]!=null && ds.Tables[0].Rows[0]["Remark"].ToString()!="")
				{
                    model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
				}
                return model;
			}
            model = null;
            return model;
		}
       public bool Add(sys_role_Model model)
       {
           int rowsAffected;
           MySqlParameter[] parameters = {
					new MySqlParameter("pRoleID", MySqlDbType.Int32,11),
					new MySqlParameter("pRoleName", MySqlDbType.VarChar,200),
					new MySqlParameter("pCreateUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pCreateDate", MySqlDbType.DateTime),
					new MySqlParameter("pRemark", MySqlDbType.VarChar,300)};
           parameters[0].Direction = ParameterDirection.Output;
           parameters[1].Value = model.RoleName;
           parameters[2].Value = model.CreateUserID;
           parameters[3].Value = model.CreateDate;
           parameters[4].Value = model.Remark;
           DbHelperMySql.RunProcedure("sys_role_ADD", parameters);
           rowsAffected = int.Parse(parameters[0].Value.ToString());
           if (rowsAffected > 0)
           {
               return true;
           }
           return false;

       }
       public bool AddUserRole(sys_userrole_Model model)
       {
           int rowsAffected;
           MySqlParameter[] parameters = {
                    new MySqlParameter("pUserRoleID", MySqlDbType.Int32,11), 
					new MySqlParameter("pUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pRoleID", MySqlDbType.VarChar,200),
					new MySqlParameter("pCreateUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pCreateDate", MySqlDbType.DateTime)};
           parameters[0].Direction = ParameterDirection.Output;
           parameters[1].Value = model.UserID;
           parameters[2].Value = model.RoleID;
           parameters[3].Value = model.CreateUserID;
           parameters[4].Value = model.CreateDate;
           DbHelperMySql.RunProcedure("sys_userrole_ADD", parameters);
           rowsAffected = int.Parse(parameters[0].Value.ToString());
           if (rowsAffected > 0)
           {
               return true;
           }
           return false;

       }
       public DataTable GetListByUserID(int UserID)
       {
           MySqlParameter[] parameters = {
					new MySqlParameter("pUserID", MySqlDbType.Int32,11)};
           parameters[0].Value = UserID;
           return DbHelperMySql.RunProcedure("sys_userrole_GetList", parameters).Tables[0];
       }
       public DataTable GetModelByUserID(int UserRoleID)
       {
           MySqlParameter[] parameters = {
					new MySqlParameter("pUserRoleID", MySqlDbType.Int32,11)};
           parameters[0].Value = UserRoleID;
           return DbHelperMySql.RunProcedure("sys_userrole_GetModel", parameters).Tables[0];
       }
       public void DeleteRoleUser(int RoleID)
       {
           MySqlParameter[] parameters = { 
                          new MySqlParameter("pUserRoleID",MySqlDbType.Int32,11)
                                         };
           parameters[0].Value = RoleID;
           DbHelperMySql.RunProcedure("sys_userrole_Delete", parameters);
       }
       public bool Update(sys_role_Model model)
       {
           int rowsAffected;
           MySqlParameter[] parameters = {
                    new MySqlParameter("rowID",MySqlDbType.Int32),
					new MySqlParameter("pRoleID", MySqlDbType.Int32,11),
					new MySqlParameter("pRoleName", MySqlDbType.VarChar,200),
					new MySqlParameter("pModifyUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pModifyDate", MySqlDbType.DateTime),
					new MySqlParameter("pRemark", MySqlDbType.VarChar,300)};
           parameters[0].Direction = ParameterDirection.Output;
           parameters[1].Value = model.RoleID;
           parameters[2].Value = model.RoleName;
           parameters[3].Value = model.CreateUserID;
           parameters[4].Value = model.CreateDate;
           parameters[5].Value = model.Remark;
           DbHelperMySql.RunProcedure("sys_role_Update", parameters);
           rowsAffected = int.Parse(parameters[0].Value.ToString());
           if (rowsAffected > 0)
           {
               return true;
           }
           return false;

       }
       public void UpdateUserRole(sys_userrole_Model model)
       {
           MySqlParameter[] parameters = {
                    new MySqlParameter("pUserRoleID", MySqlDbType.Int32,11), 
					new MySqlParameter("pUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pRoleID", MySqlDbType.VarChar,200),
					new MySqlParameter("pModifyUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pModifyDate", MySqlDbType.DateTime)};
           parameters[0].Value = model.UserRoleID;
           parameters[1].Value = model.UserID;
           parameters[2].Value = model.RoleID;
           parameters[3].Value = model.ModifyUserID;
           parameters[4].Value = model.ModifyDate;
           DbHelperMySql.RunProcedure("sys_userrole_Update", parameters);

       }
    }
}
